package com.usian.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.mapper.ApUserFanMapper;
import com.usian.user.mapper.ApUserFollowMapper;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserRelationService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserRelationServiceImpl implements ApUserRelationService {

    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private ApUserFanMapper apUserFanMapper;

    @Autowired
    private KafkaTemplate kafkaTemplate;


    @Override/*关注和取消  是apUser和apUserFollow和apUserFan 三个表和 article微服务下的 apAuthor 来实现的业务*/
    public ResponseResult follow(UserRelationDto dto) {

        /*检验参数*/
        if (dto.getOperation()<0 ||dto.getOperation()>1 ||dto.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        /*通过feign来调用  查询作者信息*/
        ApAuthor apAuthor = apAuthorFeign.findById(dto.getAuthorId());

        if (apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"关注的作者不存在");
        }
        /*通过本地线程来获取关注人里面只有一个属性就是主键*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"关注人不存在");
        }
        /*操作 关注(0)和取消(1) 逻辑   */
        if (dto.getOperation()==0){

            /*用来保存 粉丝表的  fansName  说白了就是为了查询关注人的名称*/
            ApUser apUser = apUserMapper.selectById(user.getId());
            if (apUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"关注人不存在");
            }

            /*根据userId和authorId 去查询  如果查询不到就添加 防止重新添加
                  在关注表中userId指的是  apUser的ID
                            followId指的是  作者的id
                            followName指的是 作者的名称
             */
            List<ApUserFollow> followList = apUserFollowMapper.findApUserFollow(user.getId(), apAuthor.getId());
            if (followList.size()==0){
                ApUserFollow apUserFollow = new ApUserFollow();
                apUserFollow.setUserId(user.getId());
                apUserFollow.setFollowId(dto.getAuthorId());
                apUserFollow.setFollowName(apAuthor.getName());
                apUserFollow.setCreatedTime(new Date());
                apUserFollow.setIsNotice(true);
                apUserFollow.setLevel((short)1);
                apUserFollowMapper.insert(apUserFollow);
            }
                 /*根据userId和authorId 去查询  如果查询不到就添加 防止重新添加
                  在粉丝表中userId指的是  作者的id
                            followId指的是  粉丝的id
                            followName指的是 粉丝的名称
             */
            List<ApUserFan> fanList = apUserFanMapper.findApUserFan(apAuthor.getId(), user.getId());
            if (fanList.size()==0){
                ApUserFan apUserFan =  new ApUserFan();
                apUserFan.setUserId(dto.getAuthorId());
                apUserFan.setFansId(user.getId().longValue());
                apUserFan.setFansName(apUser.getName());
                apUserFan.setLevel((short)0);
                apUserFan.setIsDisplay(true);
                apUserFan.setIsShieldLetter(false);
                apUserFan.setIsShieldComment(false);
                apUserFan.setCreatedTime(new Date());
                apUserFanMapper.insert(apUserFan);
            }

            FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();
            followBehaviorDto.setArticleId(dto.getArticleId());
            followBehaviorDto.setFollowId(dto.getAuthorId());
            followBehaviorDto.setUserId(apUser.getId());

            /*关注后 同步到 关注行为微服务*/
            kafkaTemplate.send("follow_topic", JSON.toJSONString(followBehaviorDto));

            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {

            /*删除的话 先查询 不为null则删除*/
            List<ApUserFollow> followList = apUserFollowMapper.findApUserFollow(user.getId(), apAuthor.getId());
            if(followList.size()>0){
                for (ApUserFollow apUserFollow : followList) {
                    apUserFollowMapper.deleteApUserFollow(user.getId(),apAuthor.getId());
                }
            }
            List<ApUserFan> fanList = apUserFanMapper.findApUserFan(apAuthor.getId(), user.getId());
            if(fanList.size()>0){
                for (ApUserFan apUserFan : fanList) {
                    apUserFanMapper.deleteApUserFan(apAuthor.getId(),user.getId());
                }
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }
}
